//
//  WYEnterPriserIntroductionController.m
//  Boss
//
//  Created by admin on 2018/7/5.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYEnterPriserIntroductionController.h"

@interface WYEnterPriserIntroductionController ()<UITextViewDelegate>
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;
@property (nonatomic, assign) NSInteger textH;
@property (strong, nonatomic) IBOutlet UILabel *countLabel;

@end

@implementation WYEnterPriserIntroductionController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    
}
-(void)setUpUI
{
    self.navigationItem.title = @"企业简介";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(cancelAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStyleDone) target:self action:@selector(saveAction:)];
    self.view.backgroundColor = [UIColor hexStringToColor:@"#e6e6e6" alpha:1.0f];
    self.topHeight.constant = self.navHeight;
    [self setupTextView];
    if (self.introString.length) {
        self.textView.text = self.introString;
    }else{
        
    }
    self.textView.scrollEnabled = NO;
    self.textView.scrollsToTop = NO;
    self.textView.showsHorizontalScrollIndicator = NO;
    self.textView.enablesReturnKeyAutomatically = YES;
    self.textView.delegate = self;
    //    self.layer.borderWidth = 1;
    //    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //实时监听textView值得改变
//    [[NSNotificationCenter defaultCenter] addObserver:self.textView selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    
    
}
-(void)textViewDidChange:(UITextView *)textView
{
    
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    if (existTextNum > 1000){
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [nsTextContent substringToIndex:1000];
        //[textView setAttributedText: [self textViewAttributedStr:s]];
        textView.text = s;
    }
    //不让显示负数
    self.countLabel.text = [NSString stringWithFormat:@"%ld",MAX(0,1000 - existTextNum)];
    
    
    
    NSInteger height = ceilf([self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, MAXFLOAT)].height);
    if (self.textViewHeight.constant <= height) { // 高度不一样，就改变了高度
        //_textH = height;
        self.textViewHeight.constant = height;
        
    }
    
    
    
    
    
    
}

- (void)textDidChange
{
    // 根据文字内容决定placeholderView是否隐藏
    
    NSInteger height = ceilf([self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, MAXFLOAT)].height);
    
    if (_textH != height) { // 高度不一样，就改变了高度
        _textH = height;
        self.textViewHeight.constant = height;
        
    }
}




- (void)setupTextView
{

    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"企业简介（1000字以内）";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = [UIColor lightGrayColor];
    [placeHolderLabel sizeToFit];
    [self.textView addSubview:placeHolderLabel];
    placeHolderLabel.font = [UIFont systemFontOfSize:13.f];
    [self.textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
}
                                                                        
                                                                        





-(void)cancelAction:(UIBarButtonItem *)item
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveAction:(UIBarButtonItem *)item
{
    if (!self.textView.text.length) {
        [self toast:@"请输入简介"];  return;
    }
    WEAKSELF
    [WYHttpServer editCompanyNameWithCompanyintro:self.textView.text SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshMessageDate" object:nil];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [weakSelf toast:errorMessage];
        }
    }];
    
    
    
    //[self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
